import { IBreadCrumb } from '@/base-ui/breadcrumb';
import { RouteRecordRaw } from 'vue-router';

/**
 * 保存我的第一个路由的数据
 */
let firstMenu: any = null;

/**
 * 动态配置用户权限菜单
 * @param userMenus 所有的userMenus
 * @returns 我用户有的规则配置
 */
export function mapMenusToRoutes(userMenus: any[]): RouteRecordRaw[] {
  const routes: RouteRecordRaw[] = [];

  // 1.先去加载默认所有的routes
  const allRoutes: RouteRecordRaw[] = [];
  // webpack的一个拿去文件的函数 第一个参数是路径,第二个参数是否递归,配置文件
  const routeFiles = require.context('../router/main', true, /\.ts/);
  routeFiles.keys().forEach((key) => {
    // 拿到我的路由规则路径
    const route = require('../router/main' + key.split('.')[1]);
    allRoutes.push(route.default);
  });

  // 2.根据菜单信息获取需要添加的  routes,因为存在嵌套,所以,递归就完事
  const _recurseGetRoute = (menus: any[]) => {
    for (const menu of menus) {
      if (menu.type === 2) {
        const route = allRoutes.find((route) => route.path === menu.url);
        if (route) routes.push(route);
        if (!firstMenu) firstMenu = menu;
      } else {
        _recurseGetRoute(menu.children);
      }
    }
  };

  _recurseGetRoute(userMenus);

  return routes;
}

export { firstMenu };

/**
 * 根据路径匹配我的路由数据
 * @param userMenu
 * @param currentPath
 * @returns any
 */
export function pathMapToMenu(
  userMenu: any[],
  currentPath: string,
  breadCrumbs?: IBreadCrumb[]
): any {
  for (const menu of userMenu) {
    if (menu.type === 1) {
      // 解释 ?? 就是是undefined 就是一个[]
      const temp = pathMapToMenu(menu.children ?? [], currentPath);
      if (temp) {
        breadCrumbs?.push({ name: menu.name });
        breadCrumbs?.push({ name: temp.name });
        return temp;
      }
    } else if (menu.type === 2) {
      if (menu.url == currentPath) {
        return menu;
      }
    }
  }
}
/**
 * 根据路径获取breadCrumbs数据
 * @param userMenu any[]
 * @param currentPath string
 * @returns IBreadCrumb[]
 */
export function pathMapToBreadCrumb(
  userMenu: any[],
  currentPath: string
): IBreadCrumb[] {
  const breadCrumbs: IBreadCrumb[] = [];
  pathMapToMenu(userMenu, currentPath, breadCrumbs);
  return breadCrumbs;
}

/**
 * 获取这个用户的所有权限标识
 * @param userMenus any[]
 * @returns string[]
 */
export function mapMenusToPermissions(userMenus: any[]) {
  const permissions: string[] = [];
  const _recurseGetPermission = (menus: any[]) => {
    for (const menu of menus) {
      if (menu.type === 1 || menu.type === 2) {
        _recurseGetPermission(menu.children ?? []);
      } else if (menu.type === 3) {
        permissions.push(menu.permission);
      }
    }
  };
  _recurseGetPermission(userMenus);

  return permissions;
}

/**
 * 获取菜单中所有的叶子节点的id
 * @param menuList any[]
 * @returns number[]
 */
export function getMenuLeafKeys(menuList: any[]) {
  const leafKeys: number[] = [];

  const _recurseGetLeaf = (menuList: any[]) => {
    for (const menu of menuList) {
      if (menu.children) {
        _recurseGetLeaf(menu.children);
      } else {
        leafKeys.push(menu.id);
      }
    }
  };

  _recurseGetLeaf(menuList);

  return leafKeys;
}
